package com.bawei.play.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.SimpleVideoEntity
import com.bawei.image.ImageLoadUtil
import com.bawei.image.ImageOptions
import com.bawei.play.databinding.ItemPlayVideoBinding

/**
 * Author: Jason Zhang
 * Date: 2024/3/4
 * Desc:
 */
class PlayVideoAdapter(private val list: MutableList<SimpleVideoEntity>): RecyclerView.Adapter<PlayVideoAdapter.VideoPlayViewHolder>() {

    private var itemClickListener: OnItemClickListener? = null

    //设置Item点击事件
    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickListener = listener
    }

    inner class VideoPlayViewHolder(val dataBinding: ItemPlayVideoBinding): RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoPlayViewHolder {
        return VideoPlayViewHolder(ItemPlayVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: VideoPlayViewHolder, position: Int) {
        var entity = list[position]

        //视频播放器
        val player = holder.dataBinding.gsyPlayer
        player.setUp(entity.videopath, true, "")
        player.backButton.visibility = View.GONE
        player.fullscreenButton.visibility = View.GONE

        //头像
        ImageLoadUtil.with(holder.dataBinding.root.context)
            .setOptions(ImageOptions.Builder.isCircle(true).build())
            .loadUrl(entity.avatar_url)
            .into(holder.dataBinding.ivHead)
        //评论次数
        holder.dataBinding.tvPingLunCount.text = if (entity.commentnum > 0) entity.commentnum.toString() else "1.2W"
        //播放碟片
        var playImage = holder.dataBinding.ivMusicPlayer
        ImageLoadUtil.with(holder.dataBinding.root.context)
            .setOptions(ImageOptions.Builder.isCircle(true).build())
            .loadUrl(entity.avatar_url)
            .into(playImage)

        //属性动画
        val animator = ObjectAnimator.ofFloat(playImage, "Rotation", 0f, 360f)
        animator.duration = 1500 //动画时长
        animator.repeatCount = ValueAnimator.INFINITE //无线循环
        animator.interpolator = LinearInterpolator() //匀速效果
        animator.start()

        //添加子View点击事件
        holder.dataBinding.ivHead.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivAdd.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivDianZanBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivPingLunBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivShouCangBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivShareBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivClose.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivSend.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }


    }

    override fun getItemCount(): Int = list.size

    override fun getItemViewType(position: Int): Int {
        return 0
    }

    /**
     * 点击事件接口类
     */
    interface OnItemClickListener {
        fun onItemChildClick(v: View?, position: Int)
    }


}